package com.example.demo01.controller;

import com.example.demo01.dao.DepartmentDao;
import com.example.demo01.entities.Department;
import com.example.demo01.entities.Employee;
import com.example.demo01.entities.Person;
import com.example.demo01.exception.UserNotExistException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wangxun
 */
@Controller
public class HelloController {

    private static Map<Integer, Employee> employees;

    static {
        employees = new HashMap<>();

        employees.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1, new Department(101, "D-AA")));
        employees.put(1002, new Employee(1002, "E-BB", "bb@163.com", 1, new Department(102, "D-BB")));
        employees.put(1003, new Employee(1003, "E-CC", "cc@163.com", 0, new Department(103, "D-CC")));
        employees.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0, new Department(104, "D-DD")));
        employees.put(1005, new Employee(1005, "E-EE", "ee@163.com", 1, new Department(105, "D-EE")));
    }

    @Autowired
    private Person person;

//    @RequestMapping(path = {"/", "/login.html"})
//    public String index() {
//        return "login";
//    }


    @RequestMapping(path = "/hello")
    @ResponseBody
    public String hello(@RequestParam String user) {
        if ("aaa".equals(user)) {
            throw new UserNotExistException("用户不存在");
        }
        return "Hello World!";
    }


    @RequestMapping(path = "/person")
    @ResponseBody
    public Person person() {
        return person;
    }


    @RequestMapping(path = "/success")
    public String success(Map<String, Object> map) {
        map.put("hello", "<h1>你好<h1/>");
        map.put("users", Arrays.asList("zhangsan", "lisi", "wangwu"));
        // TODO: classpath:/templates/success.html
        return "success";
    }

}
